// const fetch = require('node-fetch');
// const _ = require('lodash');
// const dayjs = require('dayjs');

import fetch from 'node-fetch';
import _ from "lodash"
import axios from "axios"
import dayjs from "dayjs"

let now = dayjs().format("YYYY/MM/DD")
let last100 = (dayjs().subtract(99, "day")).format("YYYY/MM/DD")
let last100Str = `${last100}-${now}`

let cookie="iTalent-tenantId=609303; isItalentLogin=; italentLoginSync=1702986432261; Tita_PC=HLiArb4hK02qigsZMu3RIKx1jg_6aceQI_o8KPHuCMeXUhOckvcmsvzMSpc_MsXF; ssn_Tita_PC=HLiArb4hK02qigsZMu3RIKx1jg_6aceQI_o8KPHuCMeXUhOckvcmsvzMSpc_MsXF; key-611840338=true; user_polling_timespace_609303=0"


let handleCommitList = async () => {
    let commitList = []
 await   fetch("https://cloud.italent.cn/api/v2/UI/TableList?viewName=Attendance.SingleObjectListView.EmployeeOverTime&metaObjName=Attendance.OverTime&app=Attendance&PaaS-SourceApp=Attendance&PaaS-CurrentView=Attendance.EmployeeOverTimeView", {
        "headers": {
            "accept": "application/json, application/xml, text/play, text/html, */*",
            "accept-language": "zh-CN,zh;q=0.9",
            "content-type": "application/json; charset=utf-8",
            "eagleeye-traceid": "28dae312-bb70-43ae-8f87-d729552fcebd",
            "sec-ch-ua": "\"Chromium\";v=\"21\", \" Not;A Brand\";v=\"99\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-site",
            "x-sourced-by": "ajax",
            "cookie":cookie,
            "Referer": "https://www.italent.cn/",
            "Referrer-Policy": "strict-origin-when-cross-origin"
        },
        "body": `{"table_data":{"advance":{"cmp_render":{"viewPath":"OverTimeRecordTableWithLoadingControl","status":"enable"}},"hasCheckColumn":false,"ext_data":{"ListViewLabel":"我的加班","SortCursor":""},"isEnableGlobleCheck":false,"hasRowHandler":true,"paging":{"total":0,"capacity":100,"page":0,"capacityList":[15,30,60,100]},"isAvatars":true,"viewName":"Attendance.SingleObjectListView.EmployeeOverTime","operateColumWidth":140,"extendsParam":"","isSyncRowHandler":true,"isFrozenOperationColumnHandler":false,"isCustomListViewExisted":false,"getTreeNodeUrl":null,"sort_fields":[{"sort_column":"StartTime","sort_dir":"desc"},{"sort_column":"CreatedTime","sort_dir":"desc"}],"description":"我的加班","metaObjName":"Attendance.OverTime","isCustomListView":true,"navViewIsCustom":false,"navViewName":"Attendance.EmployeeOverTimeView","navViewVersion":"20230920152531362"},"search_data":{"metaObjName":"Attendance.OverTime","searchView":"Attendance.EmployeeSearchViewForm","items":[{"name":"Attendance.OverTime.StaffId","text":"包文庆(jerry.bao@rct-power.com.cn)","value":"611840338","num":"1","metaObjName":"","metaFieldRelationIDPath":"","queryAreaSubNodes":false},{"name":"Attendance.OverTime.StdIsDeleted","text":"否","value":"0","num":"7","metaObjName":"","metaFieldRelationIDPath":"","queryAreaSubNodes":false},{"name":"Attendance.OverTime.Invalid","text":"是","value":"1","num":"10","metaObjName":"","metaFieldRelationIDPath":"","queryAreaSubNodes":false},{"name":"Attendance.OverTime.OverTimeDate","text":"${last100Str}","value":"${last100Str}","num":"2","metaObjName":"","metaFieldRelationIDPath":"","queryAreaSubNodes":false}],"searchFormFilterJson":null}}`,
        "method": "POST"

    }).then(res => {
        if (res.ok) {
            return res.json()
        }
    }).then(res => {

        commitList = res.biz_data.filter(item => !_.isEmpty(item.VestingDate))
            .map(item => item.VestingDate.value || '')

    });
    return commitList
}
let handleLast100 = async () => {
//最近100天的考勤
    let lastCard100List = []

    let body = `{"table_data":{"advance":{"cmp_render":{"viewPath":"MyAttendanceStatisticsTable","status":"enable"}},"hasCheckColumn":true,"ext_data":{"ListViewLabel":"我的考勤列表"},"isEnableGlobleCheck":false,"hasRowHandler":true,"paging":{"total":0,"capacity":100,"page":0,"capacityList":[15,30,60,100]},"isAvatars":true,"viewName":"Attendance.SingleObjectListView.EmpAttendanceDataList","operateColumWidth":140,"extendsParam":"","isSyncRowHandler":true,"isFrozenOperationColumnHandler":false,"isCustomListViewExisted":false,"getTreeNodeUrl":null,"sort_fields":[{"sort_column":"SwipingCardDate","sort_dir":"desc"}],"description":"员工出勤列表","metaObjName":"Attendance.AttendanceStatistics","isCustomListView":true,"navViewIsCustom":false,"navViewName":"Attendance.AttendanceDataRecordNavView","navViewVersion":"0"},"search_data":{"metaObjName":"Attendance.AttendanceStatistics","searchView":"Attendance.EmpAttendanceDataSearch","items":[{"name":"Attendance.AttendanceStatistics.StaffId","text":"包文庆(jerry.bao@rct-power.com.cn)","value":"611840338","num":"1","metaObjName":"","metaFieldRelationIDPath":"","queryAreaSubNodes":false},{"name":"Attendance.AttendanceStatistics.StdIsDeleted","text":"否","value":"0","num":"5","metaObjName":"","metaFieldRelationIDPath":"","queryAreaSubNodes":false},{"name":"Attendance.AttendanceStatistics.Status","text":"启用","value":"1","num":"6","metaObjName":"","metaFieldRelationIDPath":"","queryAreaSubNodes":false},{"name":"Attendance.AttendanceStatistics.SwipingCardDate","text":\"${last100Str}\","value":\"${last100Str}\","num":"","metaObjName":"","metaFieldRelationIDPath":"","queryAreaSubNodes":false}],"searchFormFilterJson":null}}`;

    await fetch("https://cloud.italent.cn/api/v2/UI/TableList?viewName=Attendance.SingleObjectListView.EmpAttendanceDataList&metaObjName=Attendance.AttendanceStatistics&app=Attendance&PaaS-SourceApp=Attendance&PaaS-CurrentView=Attendance.AttendanceDataRecordNavView", {
            "headers": {
                "accept": "application/json, application/xml, text/play, text/html, */*",
                "accept-language": "zh-CN,zh;q=0.9",
                "content-type": "application/json; charset=utf-8",
                "eagleeye-traceid": "28dae312-bb70-43ae-8f87-d729552fcebd",
                "sec-ch-ua": "\"Chromium\";v=\"21\", \" Not;A Brand\";v=\"99\"",
                "sec-ch-ua-mobile": "?0",
                "sec-ch-ua-platform": "\"Windows\"",
                "sec-fetch-dest": "empty",
                "sec-fetch-mode": "cors",
                "sec-fetch-site": "same-site",
                "x-sourced-by": "ajax",
                "cookie":cookie,
                "Referer": "https://www.italent.cn/",
                "Referrer-Policy": "strict-origin-when-cross-origin"
            },
            "method": "POST",
            "body": body
        }
    ).then(res => {

        if (res.ok) {
            return res.json()
        }
    }).then(res => {


        lastCard100List = res.biz_data.filter(item => !_.isEmpty(item.ActualForLastCard))
            .map(item => item.ActualForLastCard.value || '')
            .filter(item => item >= item.substring(0, 11) + "20:00:00" && dayjs(item) >= dayjs("2023/10/10"))
            // .map(item => dayjs(item).format("YYYY/MM/DD"))
    });

    return (lastCard100List)
}

let main = async () => {
    let lastCard100List = await handleLast100()
    let commitList = await handleCommitList()
    console.log("获取最近100天(移除启用日10-10号之前的数据)的加班日期：", lastCard100List)
    console.log("已填写的加班：", commitList)
    console.log("未填写的加班：", lastCard100List.filter(item => !commitList.some(commit=>item.includes(commit))))
}
main()
